package de.szut.dynamic.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TabHost;
import de.szut.entity.AbilityTree;
import de.szut.logic.Logic;
import de.szut.logic.ModifierStore;
import de.szut.main.ResourcesManager;
import de.szut.scene.helper.SceneManager;
import de.szut.scene.scenes.GameScene;
import de.szut.splasher.R;

/**
 * Created by Dmitrij on 13.09.13.
 */
public class AbilityTreeGenerator {

    private ModifierStore modStore;
    private ResourcesManager resourcesManager;

    private Context context;

    public AbilityTreeGenerator(Context ctx) {
        this.context = ctx;
        this.modStore = Logic.getInstance(this.context).getModifierStore();
        this.modStore.loadAvailableTrees();
        this.resourcesManager = ResourcesManager.getInstance();
    }

    public View generate() {
        View layout = this.getBaseLayout();
        TabHost tabHost = (TabHost) layout.findViewById(android.R.id.tabhost);
        ImageButton cancelBtn = (ImageButton)layout.findViewById(R.id.abilitie_menu_cancel);
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resourcesManager.getActivity().getAbilityMenu().setVisibility(View.GONE);
                ((GameScene)SceneManager.getInstance().getCurrentScene()).setSceneIsTouchable(true);
                if (((GameScene) SceneManager.getInstance().getCurrentScene()).isTimeIsRunning()) {
                    ((GameScene) SceneManager.getInstance().getCurrentScene()).reRunTimer();
                }
            }
        });
        tabHost.setup();
        for (int i =0;i<this.modStore.getAvailableTrees().size();i++) {
            AbilityTree tree = this.modStore.getAvailableTrees().get(i);
            tabHost.addTab(this.generateTabSpec(tabHost, tree));
            switch (i){
                case 0:
                    tabHost.getTabWidget().getChildTabViewAt(i).setBackgroundResource(R.drawable.circuit_bg_red);
                    break;
                case 1:
                    tabHost.getTabWidget().getChildTabViewAt(i).setBackgroundResource(R.drawable.circuit_bg_green);
                    break;
                case 2:
                    tabHost.getTabWidget().getChildTabViewAt(i).setBackgroundResource(R.drawable.circuit_bg_blue);
                    break;
            }
        }
        return layout;
    }

    private View getBaseLayout() {
        LayoutInflater inflater = LayoutInflater.from(this.context);
        return inflater.inflate(R.layout.dyn_ability_view, null);
    }

    private TabHost.TabSpec generateTabSpec(TabHost tabHost, AbilityTree tree) {
        TabHost.TabSpec tab = tabHost.newTabSpec(tree.getName());
        tab.setIndicator(tree.getName());
        tab.setContent(new AbilityTreeTabContent(this.context, tree));
        return tab;
    }


}